package com.licw.controller.admin;

import com.licw.dto.DishDTO;
import com.licw.dto.DishPageQueryDTO;
import com.licw.entity.Dish;
import com.licw.result.PageResult;
import com.licw.result.Result;
import com.licw.service.DishService;
import com.licw.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Resource
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result<Object> add(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult<DishVO>> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查看菜品：{}", dishPageQueryDTO);
        PageResult<DishVO> page = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 根据ids批量删除菜品
     */
    @DeleteMapping
    @ApiOperation("根据ids批量删除菜品")
    public Result<Object> deleteById(@RequestParam List<Long> ids){
        log.info("根据id批量删除菜品：{}",ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO = dishService.getVOById(id);
        return Result.success(dishVO);
    }
    /**
     * 修改菜品信息
     */
    @PutMapping
    @ApiOperation("修改菜品信息")
    public Result<Object> edit(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息：{}",dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 停售/起售菜品
     * @param status 状态值
     * @param id 菜品id
     */
    @PostMapping("/status/{status}")
    @ApiOperation("停售/起售菜品")
    public Result<Object> setStatus(@PathVariable Integer status, Long id){
        log.info("停售/起售菜品：{}", id);
        dishService.setStatus(status, id);
        return Result.success();
    }

}
